# -*- coding:utf-8 -*-
"""
@time       2016/11/30 下午2:34
"""
from sympy.abc import a, b
from mathsolver.functions.base import *


class IsExsNorm(BaseFunction):  # 输入一个多项式，判断是否为(a+b)^2的形式
    def solver(self, *args):
        poly = sympify(args[0].value)
        flag = False
        if isinstance(poly, Pow):
            di, mi = poly.args
            if len(di.args) == 2:
                flag = True
        if flag:
            self.steps.append(["", "该多项式是标准二项式"])
        else:
            self.steps.append(["", "该多项式不是标准二项式"])
        self.output.append(BaseVariable(flag))
        return self


class ExsNorm(BaseFunction):  # 输入一个多项式，求二项式形式
    def solver(self, *args):
        poly = args[0].sympify()
        try:
            ex_poly = BaseErXiangShi(poly)
            norm = ex_poly.norm()
            self.steps.append(["", "多项式的二项式形式为:%s" % norm.printing()])
            self.output.append(norm)
        except Exception:
            self.steps.append(["", "无法转化成为二项式。"])
        return self


class ExsCoeff(BaseFunction):
    def solver(self, *args):  # 输入poly
        poly = args[0].sympify()
        ex_poly = BaseErXiangShi(poly)
        values = BaseSymbolValue({a: ex_poly.a, b: ex_poly.b, n: ex_poly.n})
        self.steps.append(["", "二项式中：%s" % values.printing()])
        self.output.append(values)
        return self


class ExsCoeff001(BaseFunction):  # 求二项式的参数
    def solver(self, *args):  # in: coeff(a,b,n), baseSymbol_val({}), out: BasePoly
        sym_1 = args[0].value2pairs()[0]
        sym_2 = args[1].value2pairs()[0]
        for k, v in sym_2.items():
            sym_1[k] = v
        sym_val = BaseSymbolValue(sym_1)
        self.steps.append(["", "参数为：%s" % sym_val.printing()])
        self.output.append(sym_val)
        return self


class ExsExpand(BaseFunction):
    def solver(self, *args):  # 输入baseSympbol_value({a:,b:,n:})
        self.label.add("求二项式的展开式")
        coeffs = args[0].value2pairs()[0]
        ex_poly = BaseErXiangShi.instance(coeffs[a], coeffs[b], coeffs[n])
        expand = ex_poly.expand()
        self.steps.append(["", "二项式的展开式为：%s" % expand.printing()])
        self.output.append(expand)
        return self


if __name__ == '__main__':
    pass
